package com.mastercard.profile

import java.util.Date;

import com.mastercard.auth.Address
import com.mastercard.auth.User;
import com.mastercard.auth.User.Status;

class ProfileService {

    def serviceMethod() {

    }
	
	def getProfileById(id) {
		return User.findById(id)
	}
	
	def saveProfile(user){
		user.save()
	}
	
	def saveUserActivation(userActivation){
		userActivation.save()
	}
	
	def refreshUserProfile(user){
		user.refresh()
	}
	
	def updateUserProfile(id, params){
		User user = getProfileById(id)
		if(params.email){
			user.email = params.email
		}
		if(params.name){
			user.name = params.name
		}
		if(params.mobile){
			user.mobile = params.mobile
		}
		
		boolean chk
		List addrList = user.addresses
		if(null != addrList && 0 < addrList.size()){
			for(Address addrTmp: addrList){
				if(Address.Indicator.Y == addrTmp.defaultInd){
					if(params.addressLine1){
						addrTmp.addressLine1 = params.addressLine1
					}
					if(params.addressLine2){
						addrTmp.addressLine2 = params.addressLine2
					}
					if(params.addressLine3){
						addrTmp.addressLine3 = params.addressLine3
					}
					if(params.postcode){
						addrTmp.postcode = params.postcode
					}
					chk = true
					break
				}
			}
		}
		
		if(!chk){
			Address addrTmp = Address.create()
			if(params.addressLine1){
				addrTmp.addressLine1 = params.addressLine1
			}
			if(params.addressLine2){
				addrTmp.addressLine2 = params.addressLine2
			}
			if(params.addressLine3){
				addrTmp.addressLine3 = params.addressLine3
			}
			if(params.postcode){
				addrTmp.postcode = params.postcode
			}
			addrTmp.country = params.country
			addrTmp.addressType = params.addressType
			addrTmp.defaultInd = Address.Indicator.Y
			addrTmp.user = user
			
			addrList.add(addrTmp)
		}
		
		user.setAddresses(addrList)
		user.save()
	}
}
